package com.softgroup.breath.easyecgproject.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by Administrator on 2017/8/10.
 */

public class CheckBoxSex extends View {

    private int width;
    private int height;
    private Paint paintBackGround;
    private Paint paintCheck;
    private Paint paintBackGroundGray;
    private String [] checkText;

    public void setCheck(boolean check) {
        isCheck = check;
        postInvalidate();
    }

    private boolean isCheck = false;
    private OnCheckedChange onCheckedChange;

    public CheckBoxSex(Context context, AttributeSet attrs) {
        super(context, attrs);
        paintBackGround = new Paint();
        paintBackGround.setAntiAlias(true);
        paintBackGround.setColor(Color.GRAY);
        paintBackGround.setStrokeWidth(5);
        paintBackGround.setTextAlign(Paint.Align.CENTER);
        paintBackGroundGray = new Paint();
        paintBackGroundGray.setAntiAlias(true);
        paintBackGroundGray.setColor(Color.GRAY);
        paintBackGroundGray.setStrokeWidth(5);
        paintBackGroundGray.setTextAlign(Paint.Align.CENTER);
        paintCheck = new Paint();
        paintCheck.setColor(0xFFEE82EE);
        paintCheck.setAntiAlias(true);
        paintCheck.setStrokeWidth(5);
        paintCheck.setTextAlign(Paint.Align.CENTER);
        checkText=new String[]{"男","女"};
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        paintBackGround.setTextSize(height * 1 / 2);
        paintBackGroundGray.setTextSize(height * 1 / 2);
        paintCheck.setTextSize(height * 1 / 2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (isCheck) {
            canvas.drawRect(height / 2, 0, width - height / 2, height, paintBackGround);
            canvas.drawCircle(height / 2, height / 2, height / 2, paintCheck);
            canvas.drawCircle(width - height / 2, height / 2, height / 2, paintBackGround);
            canvas.drawText(checkText[0], height / 2, height * 7 / 10, paintBackGround);
            canvas.drawText(checkText[1], width - height / 2, height * 7 / 10, paintCheck);
        } else {
            canvas.drawRect(height / 2, 0, width - height / 2, height, paintBackGroundGray);
            canvas.drawCircle(height / 2, height / 2, height / 2, paintBackGroundGray);
            canvas.drawCircle(width - height / 2, height / 2, height / 2, paintCheck);
            canvas.drawText(checkText[0], height / 2, height * 7 / 10, paintCheck);
            canvas.drawText(checkText[1], width - height / 2, height * 7 / 10, paintBackGroundGray);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        isCheck = !isCheck;
        if (onCheckedChange != null) {
            onCheckedChange.onChecked(isCheck);
        }
        postInvalidate();
        return super.onTouchEvent(event);
    }

    public void setOnCheckedChange(OnCheckedChange onCheckedChange) {
        this.onCheckedChange = onCheckedChange;
    }

    public void setCheckColor(int checkColor){
        paintCheck.setColor(checkColor);
    }
    public void setBackColor(int backColor){
        paintBackGround.setColor(backColor);
    }
    public boolean isCheck() {
        return isCheck;
    }

    public String[] getCheckText() {
        return checkText;
    }

    public void setCheckText(String[] checkText) {
        this.checkText = checkText;
    }

    public interface OnCheckedChange {
        void onChecked(boolean isCheck);
    }
}
